var environment=_gl.environment||"production";
var ajaxUrl={
    "local":{
        "settimeoutTime":1400,
        "queryWX":_gl._host_res+"/res/js/data/queryWX.json",
        "updateWX":_gl._host_res+"/res/js/data/updateWX.json"
    },
    "production":{
        "settimeoutTime":0,
        "queryWX":_gl._host_api+"wxs",
        "updateWX":_gl._host_api+"wxs"
    }
};

$(function(){
    var _wxData ={};
    var _buttonFunInitIndex=null;

    btnInit();
    loadWX();
    //初始化右上角按钮组
    function btnInit(){
        _buttonFunInitIndex = _helper.buttonFunInit({"arr":[{"text":"修改","class":"update","callback":updateItem}],"index":_buttonFunInitIndex});
    }

    //
    function updateItem(){
        var loadingIndex = _helper.fridayLoadinFun();
        var data = _helper.getParameter("wxForm");
        setTimeout(function(){
            $.ajax({
                type: "POST",
                url:ajaxUrl[environment].updateWX,
                data: data,
                dataType: "json"
            }).done(function(data) {
                if(data && data.code==200){
                    _helper.ajaxMessage({"message":"修改成功！","callback":function(){
                        btnInit();
                    }});
                }else{
                    _helper.ajaxMessage({"message":(data&&data.message)||"修改失败！","callback":btnInit});
                }
            }).fail(function(){
                _helper.ajaxErrorTips({"tips":"修改失败，请稍后再试！","callback":btnInit});
            }).always(function(){
                layer.close(loadingIndex);
            });
        },ajaxUrl[environment].settimeoutTime);
    }

    //
    function setWXForm(obj){
        if(!obj) obj=_wxData;
        var $wxForm = $("#wxForm");
        $wxForm.find("input[name='name']").val(obj.name||"").attr("readonly","readonly");
        $wxForm.find("input[name='wx_no']").val(obj.wx_no||"").attr("readonly","readonly");
        $wxForm.find("input[name='domain']").val(obj.domain||"").attr("readonly","readonly");
        $wxForm.find("input[name='token']").val(obj.token||"");
        $wxForm.find("input[name='create_time']").val(_helper.timeFormat(obj.create_time)||"").attr("readonly","readonly");

    }

    //ajax查询
    function loadWX(){
        var loadingIndex = _helper.fridayLoadinFun();
        setTimeout(function(){
            $.ajax({
                type: "GET",
                url:ajaxUrl[environment].queryWX,
                dataType: "json"
            }).done(function(data) {
                if(data && data.code==200){
                    _wxData = data.data;
                    setWXForm();
                    btnInit();
                }else{
                    _helper.ajaxMessage({"message":(data&&data.message)||"加载失败！","callback":btnInit});
                }
            }).fail(function(){
                _helper.ajaxErrorTips({"tips":"加载失败，请稍后再试！","callback":btnInit});
            }).always(function(){
                layer.close(loadingIndex);
            });
        },ajaxUrl[environment].settimeoutTime);
    }

});

